package com.kgcx.schedule.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.kgcx.common.bean.ReturnData;
import com.kgcx.core.exception.BusinessException;
import com.kgcx.dbutils.core.model.ParameterMap;
import com.kgcx.schedule.domain.ScheduleReserveCfgDetailDomain;
import com.kgcx.schedule.domain.ScheduleReserveCfgDomain;
import com.kgcx.schedule.service.BaseBusinessService;
import com.kgcx.schedule.service.ScheduleReserveCfgService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(value = "预约运力配置接口", tags = "预约运力配置接口")
@RestController
@RequestMapping(value = "/v1/reserve/config")
public class ScheduleReserveCfgController extends BaseBusinessController<Long, ScheduleReserveCfgDomain> {
    @Resource
    private ScheduleReserveCfgService scheduleReserveCfg;

    @Override
    protected BaseBusinessService<Long, ScheduleReserveCfgDomain> getService() {
        return scheduleReserveCfg;
    }

    @ApiOperation(value = "查询预约运力配置列表", httpMethod = "GET", notes = "查询预约运力配置列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class), //
            @ApiImplicitParam(name = "pageNum", value = "第几页，从1开始编号", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class), //
            @ApiImplicitParam(name = "orderBy", value = "排序，比如“city_id, auto_type, status”", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class), //
            @ApiImplicitParam(name = "city_id", value = "城市ID", required = false, paramType = "query", dataType = "Long", dataTypeClass = Long.class), //
            @ApiImplicitParam(name = "auto_type", value = "车辆类型", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class), //
            @ApiImplicitParam(name = "status", value = "状态，101-启用、102-禁用、103-删除", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class) //
    })
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ReturnData<List<ScheduleReserveCfgDomain>> list() {
        ParameterMap params = $params();
        List<ScheduleReserveCfgDomain> items = getService().selectEntityListPage(params);
        ReturnData<List<ScheduleReserveCfgDomain>> resutl = new ReturnData<List<ScheduleReserveCfgDomain>>();
        resutl.setData(items);
        resutl.setTotal(params.getTotalCount());
        resutl.setPages(params.getTotalPage());
        resutl.setPageNum(params.getPageNo());
        return resutl;
    }

    @ApiOperation(value = "查询预约运力配置明细列表", httpMethod = "GET", notes = "查询预约运力配置明细列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class), //
            @ApiImplicitParam(name = "pageNum", value = "第几页，从1开始编号", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class), //
            @ApiImplicitParam(name = "orderBy", value = "排序，比如“city_id, auto_type desc, begin_time asc”", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class), //
            @ApiImplicitParam(name = "city_id", value = "城市ID", required = false, paramType = "query", dataType = "Long", dataTypeClass = Long.class), //
            @ApiImplicitParam(name = "auto_type", value = "车辆类型", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class), //
            @ApiImplicitParam(name = "status", value = "状态，101-启用、102-禁用、103-删除", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class) //
    })
    @RequestMapping(value = "/listDetail", method = RequestMethod.GET)
    public ReturnData<List<ScheduleReserveCfgDetailDomain>> listDetail() {
        ParameterMap params = $params();
        List<ScheduleReserveCfgDetailDomain> items = scheduleReserveCfg.selectDetailListPage(params);
        ReturnData<List<ScheduleReserveCfgDetailDomain>> resutl = new ReturnData<List<ScheduleReserveCfgDetailDomain>>();
        resutl.setData(items);
        resutl.setTotal(params.getTotalCount());
        resutl.setPages(params.getTotalPage());
        resutl.setPageNum(params.getPageNo());
        return resutl;
    }

    @ApiOperation(value = "查询预约运力配置详情", httpMethod = "GET", notes = "查询预约运力配置详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "kid", value = "记录ID", required = true, paramType = "query", dataType = "Long", dataTypeClass = Long.class) //
    })
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public ReturnData<ScheduleReserveCfgDomain> get() {
        return new ReturnData<ScheduleReserveCfgDomain>(getService().selectEntity($long("kid")));
    }

    @ApiOperation(value = "增加预约运力配置", httpMethod = "POST", notes = "增加预约运力配置")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ReturnData<ScheduleReserveCfgDomain> add(
            @ApiParam(name = "cfg", value = "预约运力配置对象", required = true) @RequestBody ScheduleReserveCfgDomain domain //
    ) {
        try {
            getService().insertEntity(domain);
            return new ReturnData<ScheduleReserveCfgDomain>(domain);
        } catch (BusinessException e) {
            return new ReturnData<ScheduleReserveCfgDomain>(String.valueOf(e.getCode()), e.getMessage());
        }
    }

    @ApiOperation(value = "修改预约运力配置", httpMethod = "POST", notes = "修改预约运力配置")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ReturnData<ScheduleReserveCfgDomain> update(
            @ApiParam(name = "cfg", value = "预约运力配置对象", required = true) @RequestBody ScheduleReserveCfgDomain domain //
    ) {
        // 唯一键字段不让修改
        domain.setCity_id(null);
        domain.setAuto_type(null);

        try {
            getService().updateEntity(domain);
            return new ReturnData<ScheduleReserveCfgDomain>(domain);
        } catch (BusinessException e) {
            return new ReturnData<ScheduleReserveCfgDomain>(String.valueOf(e.getCode()), e.getMessage());
        }
    }

    @ApiOperation(value = "删除预约运力配置", httpMethod = "POST", notes = "删除预约运力配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "kid", value = "记录ID", required = true, paramType = "query", dataType = "Long", dataTypeClass = Long.class) //
    })
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public ReturnData<Object> delete() {
        getService().deleteEntity($long("kid"));
        return new ReturnData<Object>();
    }
}
